let stuid = window.location.hash.substring(1);
console.log(stuid);
let clazz = document.getElementById("class");
let name = document.getElementById("name");
let sexlist = document.getElementsByName("sex");
let age = document.getElementById("age");
let hobbylist = document.getElementsByName("aihao");
let tel = document.getElementById("tel");
let addresslist = document.getElementsByTagName("option");
let remark = document.getElementById("beizhu");
let date = document.getElementById("date");

getStudent(stuid)
function getStudent(stuid) {
    let pro = getAjax("http://localhost:3008/api/student/getStudent?id=" + stuid);
    pro.then((data) => {
        // data为返回过来的数据
        let stu = JSON.parse(data)[0];
        console.log(stu);
        // 数据回显
        // 性别回显
        stu.gender == "男" ? sexlist[0].checked = true : sexlist[1].checked = true;
        // 班级回显
        clazz.value = stu.clazz;
        // 名字回显
        name.value = stu.name;
        // 年龄回显
        age.value = stu.age;
        // 爱好回显
        let hobby = stu.hobby;
        if (typeof (hobby) == "object") {
            if (hobby.includes("吃饭")) {
                hobbylist[0].checked = true;
            }
            if (hobby.includes("睡觉")) {
                hobbylist[1].checked = true;
            }
            if (hobby.includes("学习")) {
                hobbylist[2].checked = true;
            }
        } else if (typeof (hobby) == "string") {
            let stringhobby = hobby.split(",");
            if (stringhobby.includes("吃饭")) {
                hobbylist[0].checked = true;
            }
            if (stringhobby.includes("睡觉")) {
                hobbylist[1].checked = true;
            }
            if (stringhobby.includes("学习")) {
                hobbylist[2].checked = true;
            }
        }

        // 手机号回显
        tel.value = stu.tel;
        // 地址回显
        if (stu.address = "郑州") {
            addresslist[0].selected = true;
        }
        if (stu.address = "洛阳") {
            addresslist[1].selected = true;
        } else {
            addresslist[2].selected = true;
        }
        // 备注回显
        remark.value = stu.remark;
        // 时间回显
        date.value = stu.date;
    })
}
function modifyStudent() {
    // 获取性别
    let gender;
    sexlist[0].checked == true ? gender = "男" : gender = "女";
    // 获取爱好
    let hobby = [];
    for (let i = 0; i < hobbylist.length; i++) {
        if (hobbylist[i].checked == true) {
            hobby.push(hobbylist[i].value);
        }
    }
    // 获取地址
    let address;
    for (let i = 0; i < addresslist.length; i++) {
        if (addresslist[i].selected == true) {
            address = addresslist[i].value;
        }
    }
    // 获取时间

    // 调用Ajax
    let add = postAjax("http://localhost:3008/api/student/updateStudent", `id=${stuid}&clazz=${clazz.value}&name=${name.value}&gender=${gender}&age=${age.value}&hobby=${hobby}&tel=${tel.value}&address=${address}&remark=${beizhu.value}&date=${date.value}`);
    add.then((data) => {
        alert("修改成功");
        location.href = './home.html'
    }).catch(data => {
        alert("修改成功");

    })

}

